package com.dha.asynctask;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.dha.variable.ThongBaoVariable;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.Toast;

public class DangThongBaoAsyncTask extends AsyncTask<Void, Void, Boolean> {
	private ProgressDialog dialog;
	private Activity activity;
	private String maSo, tieuDe, noiDung;

	public DangThongBaoAsyncTask(Activity activity, String maSo, String tieuDe,
			String noiDung) {
		super();
		this.activity = activity;
		this.maSo = maSo;
		this.tieuDe = tieuDe;
		this.noiDung = noiDung;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		if (result) {
			Toast.makeText(activity, "Đăng thông báo thành công",
					Toast.LENGTH_LONG).show();
			dialog.dismiss();
			activity.finish();
		} else {
			Toast.makeText(activity,
					"Đăng thông báo không thành công\nVui lòng thử lại",
					Toast.LENGTH_LONG).show();
			dialog.dismiss();
		}
		super.onPostExecute(result);
	}

	@Override
	protected void onPreExecute() {
		dialog = ProgressDialog.show(activity, "Vui lòng chờ",
				"Đang gởi thông báo");
		super.onPreExecute();
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		try {
			SoapObject request = new SoapObject(ThongBaoVariable.NAME_SPACE,
					ThongBaoVariable.METHOD_NAME_DANGTHONGBAO);
			PropertyInfo maGiaoVienInfo = new PropertyInfo();
			maGiaoVienInfo.setName("MaGiaoVien");
			maGiaoVienInfo.setValue(maSo);

			PropertyInfo tieuDeInfo = new PropertyInfo();
			tieuDeInfo.setName("TieuDe");
			tieuDeInfo.setValue(tieuDe);

			PropertyInfo noiDungInfo = new PropertyInfo();
			noiDungInfo.setName("NoiDung");
			noiDungInfo.setValue(noiDung);

			request.addProperty(maGiaoVienInfo);
			request.addProperty(tieuDeInfo);
			request.addProperty(noiDungInfo);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			HttpTransportSE httpTransportSE = new HttpTransportSE(
					ThongBaoVariable.URL);
			httpTransportSE.call(ThongBaoVariable.NAME_SPACE
					+ ThongBaoVariable.METHOD_NAME_DANGTHONGBAO, envelope);
			
			SoapPrimitive respone = (SoapPrimitive) envelope.getResponse();

			if (respone.toString().equals("true")){
				return true;
			}else{
				return false;
			}
			
		} catch (Exception e) {
			return false;
		}
	}

}
